$(function () {
    $.ajax({
        url: BigNews.category_list,
        type: 'get',
        dataType: 'json',
        success: function (backData) {
            console.log(backData)
            $('tbody').html(template('cat-list', backData))
        }
    })
    $('#myModal').on('show.bs.modal', function (e) {
        console.log(e.relatedTarget)
        //判断点击事件源
        console.log($(e.relatedTarget).text());
        if ($(e.relatedTarget).text() == '编辑') {
            $('.btn-confirm').text('编辑')
            $('.btn-confirm').attr('data-id', $(e.relatedTarget).attr('data-id'))
            $('#recipient-name').val($(e.relatedTarget).parent().prev().prev().text())
            $('#message-text').val($(e.relatedTarget).parent().prev().text())
            // console.log($('.btn-confirm'));
        } else {
            $('.btn-confirm').text('新增')
        }
    })
    //取消按钮点击事件
    $('.btn-cancel').click(function () {
        $('form')[0].reset()
        // console.log(11);
    })
    $('.btn-confirm').click(function () {
        console.log(this);
        if ($(this).text() == '新增') { //新增按钮事件
            $.ajax({
                url: BigNews.category_add,
                type: 'post',
                dataType: 'json',
                data: {
                    name: $('#recipient-name').val(),
                    slug: $('#message-text').val()
                },
                success: function (backData) {
                    console.log(backData);
                    if (backData.code == 201) {
                        alert('新增成功')
                        window.location.reload()
                    } else {
                        alert('新增失败')
                    }
                }
            })
        } else {
            //如果运行到这说明，这是编辑按钮事件
            $.ajax({
                url: BigNews.category_edit,
                type: 'post',
                dataType: 'json',
                data: {
                    id: $(this).attr('data-id'),
                    name: $('#recipient-name').val(),
                    slug: $('#message-text').val()
                },
                success: function (backData) {
                    console.log(backData);
                    if (backData.code == 200) {
                        alert(backData.msg)
                        window.location.reload()
                    } else {
                        alert(backData.msg)
                    }
                }

            })
        }

    })
    //删除按钮
    $('.table>tbody').on('click', '.btn-delete', function () {

        $.ajax({
            url: BigNews.category_delete,
            type: 'post',
            dataType: 'json',
            data: {
                id: $(this).attr('data-id')
            },
            success: function (backData) {
                console.log(backData);
                if (backData.code == 204) {
                    alert(backData.msg)
                    window.location.reload()
                } else {
                    alert(backData.msg)
                }
            }
        })
    })

})